﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using TMCM;

namespace Plotter
{
    public partial class PlotterUI : Form
    {
        private Plotter _plotter;
        private Axis _printer_axis;

        public PlotterUI()
        {
            InitializeComponent();            
        }

        void _plotter_Drawn()
        {
            this._plotter.XAxis.MotorStop();
            this._plotter.XAxis.Close();
            this._plotter.XAxis.Open();

            this._plotter.YAxis.MotorStop();
            this._plotter.YAxis.Close();
            this._plotter.YAxis.Open();

            this._plotter.XAxis.MoveToPosition(TMCM.TMCM.MVP_TYPE.ABS, 5000, TMCM.TMCM.WAIT_TYPE.POS, 0);
            this._plotter.YAxis.MoveToPosition(TMCM.TMCM.MVP_TYPE.ABS, 5000, TMCM.TMCM.WAIT_TYPE.POS, 0);
        }

        private void InitializePlotter(string x_port_name, string y_port_name, Axis printer_axis)
        {
            TMCM1110 tmcm_1 = new TMCM1110(x_port_name);
            TMCM1110 tmcm_2 = new TMCM1110(y_port_name);
            tmcm_1.Close();
            tmcm_2.Close();
            try
            {
                tmcm_1.Open();
                tmcm_2.Open();
                this._plotter = new Plotter(tmcm_1, tmcm_2, 400);

                this._plotter.PrinterAxis = tmcm_1;
                if (printer_axis == Axis.YAxis)
                {
                    this._plotter.PrinterAxis = tmcm_2;
                }
                this._plotter.Drawn += new Action(_plotter_Drawn);
            }
            catch (Exception e) { throw e; }
        }

        private void Test_X_Btn_Click(object sender, EventArgs e)
        {
            string port = X_Axis_Combo.Items[X_Axis_Combo.SelectedIndex].ToString();
            TMCM1110 temp = new TMCM1110(port);
            temp.Open();
            temp.MoveToPosition(TMCM.TMCM.MVP_TYPE.REL, 50000, TMCM.TMCM.WAIT_TYPE.POS, 0);
            temp.MoveToPosition(TMCM.TMCM.MVP_TYPE.REL, -50000, TMCM.TMCM.WAIT_TYPE.POS, 0);

            temp.Close();
        }

        private void Test_Y_Btn_Click(object sender, EventArgs e)
        {
            string port = Y_Axis_Combo.Items[Y_Axis_Combo.SelectedIndex].ToString();
            TMCM1110 temp = new TMCM1110(port);
            temp.Open();
            temp.MoveToPosition(TMCM.TMCM.MVP_TYPE.REL, 50000, TMCM.TMCM.WAIT_TYPE.POS, 0);
            temp.MoveToPosition(TMCM.TMCM.MVP_TYPE.REL, -50000, TMCM.TMCM.WAIT_TYPE.POS, 0);

            temp.Close();
        }

        private void Initialize_Btn_Click(object sender, EventArgs e)
        {
            this._plotter.Initialize();

            this.Rectangle_Btn.Enabled = true;
        }

        private void Save_Btn_Click(object sender, EventArgs e)
        {
            string x_port_name = X_Axis_Combo.Items[X_Axis_Combo.SelectedIndex].ToString();
            string y_port_name = Y_Axis_Combo.Items[Y_Axis_Combo.SelectedIndex].ToString();

            this.InitializePlotter(x_port_name, y_port_name, this._printer_axis);
            Initialize_Btn.Enabled = true;
        }

        private void Printer_Test_Btn_Click(object sender, EventArgs e)
        {
            string port = X_Axis_Combo.Items[X_Axis_Combo.SelectedIndex].ToString();
            TMCM1110 temp = new TMCM1110(port);
            temp.Open();
            temp.SetOutput(TMCM1110.SIO_TYPE.DOUT2, 2, 1);
            Thread.Sleep(2000);
            temp.SetOutput(TMCM1110.SIO_TYPE.DOUT2, 2, 0);

            temp.Close();
            temp.Dispose();

            this._printer_axis = Axis.XAxis;
        }

        private void Printer_Test_Y_Btn_Click(object sender, EventArgs e)
        {
            string port = Y_Axis_Combo.Items[Y_Axis_Combo.SelectedIndex].ToString();
            TMCM1110 temp = new TMCM1110(port);
            temp.Open();
            temp.SetOutput(TMCM1110.SIO_TYPE.DOUT2, 2, 1);
            Thread.Sleep(2000);
            temp.SetOutput(TMCM1110.SIO_TYPE.DOUT2, 2, 0);

            temp.Close();
            temp.Dispose();

            this._printer_axis = Axis.YAxis;
        }

        private void Draw_Rect_Btn_Click(object sender, EventArgs e)
        {
            DrawableObject drawable = new DrawableObject(ObjectPoints.GetSquare(), GetScewFactor());

            this._plotter.Draw(drawable);
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            Trackbar_Value.Text = String.Format("{0}%", 2 * trackBar1.Value);
        }

        private void Circle_Btn_Click(object sender, EventArgs e)
        {
            DrawableObject drawable = new DrawableObject(ObjectPoints.GetCircle(), GetScewFactor());
            this._plotter.Draw(drawable);
        }

        private void Triangle_Btn_Click(object sender, EventArgs e)
        {
            DrawableObject drawable = new DrawableObject(ObjectPoints.Triangle(), GetScewFactor());
            this._plotter.Draw(drawable);
        }

        private void Reset_Btn_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem((o) =>
            {
                this._plotter.XAxis.MoveToPosition(TMCM.TMCM.MVP_TYPE.ABS, 5000, TMCM.TMCM.WAIT_TYPE.POS, 0);
            });
            ThreadPool.QueueUserWorkItem((o) =>
            {
                this._plotter.YAxis.MoveToPosition(TMCM.TMCM.MVP_TYPE.ABS, 5000, TMCM.TMCM.WAIT_TYPE.POS, 0);
            });   
        }


        private double GetScewFactor()
        {
            return trackBar1.Value / 100.0 * 2.0;
        }

        private List<Point> polygon_points = new List<Point>();
        private void AddCoordBtn_Click(object sender, EventArgs e)
        {
            String[] coordinates = CoordTB.Text.Split(new Char[] { '/' });
            int x, y;
            try
            {
                x = (int)double.Parse(coordinates[0]);
                y = (int)double.Parse(coordinates[1]);

                polygon_points.Add(new Point(x, y));
                listBox1.Items.Add(String.Format("X: {0}, Y: {1}", x, y));

                CoordTB.Text = "";
            }
            catch (Exception)
            {
                MessageBox.Show("Ungültige Eingabe", "Fehler");
            }
        }

        private void ClearCoordsBtn_Click(object sender, EventArgs e)
        {
            polygon_points.Clear();
            listBox1.Items.Clear();
        }

        private void DrawPolyBtn_Click(object sender, EventArgs e)
        {
            DrawableObject drawable = new DrawableObject(polygon_points.ToArray(), GetScewFactor());
            this._plotter.Draw(drawable);
        }

    }
}
